package eu.itcsolutions.android.tutorial;
 
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;


 public class SelectieRuta extends Activity implements View.OnClickListener, DialogInterface.OnClickListener, OnKeyListener  {
	 
	 EditText txtItem;
	 Button btnAdd;
	 ListView listItems;
	 
	 ArrayList<String> toDoItems;
	 ArrayAdapter<String> aa;
	 
	 String[] StructuraProduse = { "Fidea" };
	 String currentMenu;
	 
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectieruta);
        
        
        txtItem = (EditText)findViewById(R.id.txtItem);
        btnAdd = (Button)findViewById(R.id.btnAdd);
        listItems = (ListView)findViewById(R.id.listItems);
        
        btnAdd.setOnClickListener(this);
        txtItem.setOnKeyListener(this);
        
        toDoItems = new ArrayList<String>();
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, toDoItems);
        listItems.setAdapter(aa);
        
     }
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuItem item;
    	item = menu.add("Stergere Produs");
    	item = menu.add("Structura Produse");
    	
    	return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
    	super.onOptionsItemSelected(item);
    	if (item.hasSubMenu()  == false) {
    		if (item.getTitle() == "Structura Produse") {
    			currentMenu = "Structura Produse";
    			this.displayPopup("Structura Produse", this.StructuraProduse);
    		}
    		if (item.getTitle() == "Stergere Produs") {
    			int index = listItems.getSelectedItemPosition();
    			this.deleteItem(index);
    		}
    	}
    	return true;
    }
    
    
    private void addItem(String item) {
    	if (item.length() > 0) {
    		this.toDoItems.add(item);
    		this.aa.notifyDataSetChanged();
    		this.txtItem.setText("");
    	}
    }
    
    private void deleteItem(int itemId) {
    	if (itemId >=0) {
    		String itemName = (String)listItems.getItemAtPosition(itemId);
    		Toast.makeText(getApplicationContext(), itemName+ "Stergere Produs", Toast.LENGTH_SHORT).show();
    		this.toDoItems.remove(itemId);
    		aa.notifyDataSetChanged();
    	}
    }
    
    private void displayPopup(String title, String[] items) {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle(title);
    	builder.setItems(items, this);
    	builder.show();
    }


	@Override
	public void onClick(View v) {
		if (v == this.btnAdd) {
			this.addItem(this.txtItem.getText().toString());
		}
		
	}


	public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        	this.addItem(this.txtItem.getText().toString());
        }
		return false;
		
		
	}
	
	@Override
	public void onClick(DialogInterface dialog, int item) {
		if (currentMenu == "Structura Produse") this.addItem(StructuraProduse[item]);
		
	}
}
